package com.pingan.haofang.searchcloud.common.support;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取Ip地址
 *
 * @author ZHANGBI617
 * @date 2017/3/10
 * @since 1.0.0
 */
public class IpUtils {

    /**
     * 获取用户IP
     *
     * @param request 请求
     * @return IP地址
     */
    public static String getClientIp(HttpServletRequest request) {
        // HAProxy 默认会将IP放到x-forwarded-for头
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
